/**
 * Copyrights Christophe Cornu 2010, All rights reserved.
 */
package com.cctl.speaker;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import util.Util;

import com.cctl.speaker.match.Match;
import com.cctl.speaker.match.Match1;

public class Speaker {

	public static class Result {
		public float score;
		public Sentence sentence;
		
		@Override
		public String toString() {
			return score+": "+sentence.toString();
		}

		@Override
		public int hashCode() {
			final int prime = 31;
			int result = 1;
			result = prime * result + Float.floatToIntBits(score);
			result = prime * result
					+ ((sentence == null) ? 0 : sentence.hashCode());
			return result;
		}

		@Override
		public boolean equals(Object obj) {
			if (this == obj)
				return true;
			if (obj == null)
				return false;
			if (getClass() != obj.getClass())
				return false;
			final Result other = (Result) obj;
			if (Float.floatToIntBits(score) != Float
					.floatToIntBits(other.score))
				return false;
			if (sentence == null) {
				if (other.sentence != null)
					return false;
			} else if (sentence != other.sentence)
				return false;
			return true;
		}
		
	}	

	public Map<String, String> dictionary = new HashMap<String, String>(); // Store all
																	// the words
																	// used to
																	// form
																	// sentences
	public Set<Sentence> book = new HashSet<Sentence>(); // Store all the sentences

	public List<Result> getMatches(Match match, Sentence input) {
		List<Result> results = new ArrayList<Result>();
		match.setUserInput(input);
		for (Sentence sentence : book) {
			Result result = match.calculateCorrelation(sentence);
			if (result != null)
				results.add(result);
		}
		Collections.sort(results, new Comparator<Result>() {
			public int compare(Result o1, Result o2) {
				if (o1.score < o2.score)
					return 1;
				if (o2.score > o2.score)
					return -1;
				return 0;
			}
		});
		return results;
	}
	
	public static int indexOf(String word, String[] words, boolean ignoreCaseAccents) {
		for (int i=0; i< words.length; i++) {
			if (word == words[i])
				return i;
			if (ignoreCaseAccents) {
				if (Util.convertNonAscii(word.toLowerCase()).equals(Util.convertNonAscii(words[i].toLowerCase())))
					return i;
			}
		}
		return -1;
	}
	
	public static void main(String[] args) {
		Speaker speaker = new Speaker();		
		speaker.book.add(new Sentence(new String[] {"i", "am", "happy"}, speaker.dictionary, true));
		speaker.book.add(new Sentence(new String[] {"i", "am", "sad"}, speaker.dictionary, true));
		Match match = new Match1();
		List<Result> results = speaker.getMatches(match, new Sentence(new String[] {"i", "am"}, speaker.dictionary, false));
		for (Result result : results) {
			System.out.println(result);
		}
	}

}
